
package com.iplant.basic.controller.bms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iplant.base.po.excel.ExcelData;
import com.iplant.base.utils.excel.ExcelReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.po.enums.UserGrads;
import com.iplant.base.utils.BaseConfigConstans;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.SessionConstants;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.DesUtil;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.BFCService;
import com.iplant.basic.service.BMSService;
import com.iplant.basic.service.po.bfc.BFCHomePageModule;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.utils.Constants;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author ShrisJava
 */
@RestController
@RequestMapping("/api/User")
public class UserController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    BMSService wBMSService;

    @Autowired
    BFCService wBFCService;

    @PostMapping("/Login")
    Object login(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            int wUserID = 0;
            BMSEmployee wBMSEmployeeOld = GetSession(request);
            if (wBMSEmployeeOld != null && wBMSEmployeeOld.getID() > 0) {
                wUserID = wBMSEmployeeOld.getID();
            }
            String wLoginName = wParam.containsKey("user_id") ? StringUtils.parseString(wParam.get("user_id")) : "";
            String wLoginName_Name = wParam.containsKey("user_name") ? StringUtils.parseString(wParam.get("user_name"))
                    : "";
            String wPassword = wParam.containsKey("passWord") ? StringUtils.parseString(wParam.get("passWord")) : "";

            String wToken = wParam.containsKey("token") ? StringUtils.parseString(wParam.get("token")) : "";

            long wMac = wParam.containsKey("PhoneMac") ? StringUtils.parseLong(wParam.get("PhoneMac")) : 0L;

            int wNetJS = wParam.containsKey("netJS") ? StringUtils.parseInt(wParam.get("netJS")) : 0;

            BMSEmployee wBMSEmployee = new BMSEmployee();

            if (StringUtils.isNotEmpty(wLoginName_Name)) {
                wBMSEmployee = wBMSService.BMS_LoginEmployeeByUserName(wLoginName_Name, wPassword).getResult();
            }

            if (wBMSEmployee.ID <= 0 && wBMSEmployee.ID != -100) {
                if (wNetJS > 0) {
                    // 解密
                    wLoginName = DesUtil.decrypt(wLoginName, SessionConstants.appSecret);
                    wPassword = DesUtil.decrypt(wPassword, SessionConstants.appSecret);
                    wBMSEmployee = wBMSService.BMS_LoginEmployee(wLoginName, wPassword, wMac).getResult();

                } else {
                    if (StringUtils.isNotEmpty(wPassword) && wPassword.length() > 5) {
                        wBMSEmployee = wBMSService.BMS_LoginEmployee(wLoginName, wPassword, wMac).getResult();
                    } else if (StringUtils.isNotEmpty(wToken) && wToken.length() > 5) {

                        wBMSEmployee = wBMSService.BMS_LoginEmployeeByToken(wLoginName, wToken).getResult();
                    }

                    if (wBMSEmployee.ID <= 0 && wBMSEmployee.ID != BMSEmployee.SysAdmin.ID) {

                        // LdapContext adLogin = AppDomainUtils.adLogin(wLoginName, wPassword);
                        // if (adLogin != null) {
                        //
                        // wBMSEmployee = wBMSService.BMS_LoginEmployeeByOne(wLoginName).getResult();
                        //
                        // }
                    }
                }
            }

            if (wUserID > 0 && wUserID != wBMSEmployee.ID && wNetJS <= 0) {
                wResult = GetResult(RetCode.LOGIN_ERR_CODE_LOGIN_FAIL, "");
                return wResult;
            }

            if (wBMSEmployee.ID <= 0 && wBMSEmployee.ID != -100) {

                if (wBMSEmployee.ID == -1) {
                    wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNLOGIN_ALARM);
                } else if (wBMSEmployee.ID == -2) {
                    wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNLOGIN_ALARM_NOPD);
                } else if (wBMSEmployee.ID == -5) {
                    wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNLOGIN_ALARM_NOMAC);
                } else if (wBMSEmployee.ID == -10) {
                    wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNLOGIN_ALARM_TOKEN);
                } else {
                    wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNLOGIN_ALARM);
                }

                return wResult;
            }

            SetSession(request, wBMSEmployee);
            SetCookie(request, response, wBMSEmployee);
            @SuppressWarnings("unchecked")
            Map<String, Object> wTempResult = CloneTool.Clone(wBMSEmployee, Map.class);
            wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBMSEmployee);
            wResult.putAll(wTempResult);

        } catch (Exception e) {
            wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_IN);
            logger.error(e.toString());
        }
        return wResult;

    }

    @PostMapping("/LoginByUserName")
    Object LoginByUserName(HttpServletRequest request, HttpServletResponse response,
                           @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            String wLoginID = wParam.containsKey("username") ? StringUtils.parseString(wParam.get("username")) : "";

            BMSEmployee wBMSEmployee = new BMSEmployee();

            if (StringUtils.isNotEmpty(wLoginID)) {
                wBMSEmployee = wBMSService.BMS_LoginEmployeeByOne(wLoginID).getResult();
            }

            if (wBMSEmployee.ID <= 0 && wBMSEmployee.ID != -100) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, StringUtils.Format("用户： {0}不存在！", wLoginID));
                return wResult;
            }

            SetSession(request, wBMSEmployee);
            SetCookie(request, response, wBMSEmployee);
            @SuppressWarnings("unchecked")
            Map<String, Object> wTempResult = CloneTool.Clone(wBMSEmployee, Map.class);
            wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBMSEmployee);
            wResult.putAll(wTempResult);
        } catch (Exception e) {
            wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_IN);
            logger.error(e.toString());
        }
        return wResult;

    }

    @PostMapping("/Logout")
    Object Logout(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            RemoveSession(request);
            RemoveCookie(request, response);
            wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
        } catch (Exception e) {
            wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, e.toString());
            logger.error(e.toString());
        }
        return wResult;
    }

    @GetMapping("/Info")
    Object Info(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wLoginID = wBMSEmployee.getID();

            int wUserID = StringUtils.parseInt(request.getParameter("user_info"));
            if (wUserID <= 0)
                wUserID = wLoginID;
            ServiceResult<BMSEmployee> wServerRst = wBMSService.BMS_QueryEmployeeByID(wBMSEmployee, wUserID);

            if (StringUtils.isEmpty(wServerRst.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst.getResult());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), null, wServerRst.getResult());
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/RetrievePassword")
    Object RetrievePassword(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            List<BMSEmployee> wBMSEmployeeList = CloneTool.CloneArray(wParam.get("data"), BMSEmployee.class);

            if (wBMSEmployeeList == null || wBMSEmployeeList.size() < 1) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_IN);
                return wResult;
            }
            ServiceResult<Integer> wServerRst = new ServiceResult<Integer>(0);

            List<BMSEmployee> wEngineerUserList = Constants.GetEngineerUserList();
            Optional<BMSEmployee> wOptional = null;
            BMSEmployee wEngineerUser = null;
            UserGrads wUserGrads = UserGrads.Default;

            for (BMSEmployee wBMSEmployeeTemp : wBMSEmployeeList) {
                if (wBMSEmployeeTemp.ID <= 0)
                    continue;

                wUserGrads = UserGrads.getEnumType(wBMSEmployeeTemp.Grad);

                if (wBMSEmployeeTemp.ID > Constants.EngineerUserIDMin || wUserGrads == UserGrads.Engineer
                        || wUserGrads == UserGrads.System) {

                    wOptional = wEngineerUserList.stream().filter(p -> p.ID == wBMSEmployeeTemp.ID).findFirst();
                    if (wOptional.isPresent()) {
                        wEngineerUser = wOptional.get();
                        wEngineerUser.Password = DesUtil.encrypt("123456", SessionConstants.appSecret);
                        Constants.SetEngineerUserList(wEngineerUserList);
                    }
                }
                wServerRst = wBMSService.BMS_ResetPassword(wBMSEmployee, wBMSEmployeeTemp);
                if (StringUtils.isNotEmpty(wServerRst.getFaultCode()))
                    break;

            }
            if (wEngineerUser != null) {
                Constants.SetEngineerUserList(wEngineerUserList);
            }

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/PasswordModify")
    Object PasswordModify(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {

        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = null;

            String wUserAcc = StringUtils.parseString(wParam.get("Id"));
            String wPassword = StringUtils.parseString(wParam.get("PassWord"));
            String wOldPassWord = StringUtils.parseString(wParam.get("OldPassWord"));

            if (StringUtils.isEmpty(wPassword) || wPassword.trim().length() < 6 || wPassword.trim().length() > 12) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, "新密码不符合标准！（长度6-12的字母和数字）");
                return wResult;
            }

            if (StringUtils.parseInt(wUserAcc) > Constants.EngineerUserIDMin) {

                List<BMSEmployee> wEngineerUserList = Constants.GetEngineerUserList();
                Optional<BMSEmployee> wOptional = wEngineerUserList.stream()
                        .filter(p -> p.LoginName.equalsIgnoreCase(wUserAcc) && p.Password
                                .equalsIgnoreCase(DesUtil.encrypt(wOldPassWord, SessionConstants.appSecret)))
                        .findFirst();

                if (wOptional.isPresent()) {
                    wBMSEmployee = wOptional.get();

                    wBMSEmployee.Password = DesUtil.encrypt(wPassword, SessionConstants.appSecret);

                    Constants.SetEngineerUserList(wEngineerUserList);

                    wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBMSEmployee);
                } else {
                    wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNLOGIN_ALARM);
                }

                return wResult;
            }

            ServiceResult<BMSEmployee> wServerRst = wBMSService.BMS_LoginEmployee(wUserAcc, wOldPassWord, 0);

            if (wServerRst.getResult() != null) {
                wBMSEmployee = wServerRst.getResult();
                if (wBMSEmployee.ID <= 0) {
                    if (wBMSEmployee.ID == -1) {
                        wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNLOGIN_ALARM);
                    } else if (wBMSEmployee.ID == -2) {
                        wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNLOGIN_ALARM_NOPD);
                    } else if (wBMSEmployee.ID == -5) {
                        wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNLOGIN_ALARM_NOMAC);
                    } else {
                        wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_IN);
                    }

                    return wResult;
                }

                wBMSEmployee.Password = wPassword;

                ServiceResult<Integer> wServiceResult = wBMSService.BMS_SavePassword(wBMSEmployee, wBMSEmployee);
                wServerRst.FaultCode += wServiceResult.getFaultCode();
            }

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBMSEmployee);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), null, wBMSEmployee);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_SUC, ex.toString(), null, null);
        }
        return wResult;
    }

    @GetMapping("/All")
    Object All(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            int wActive = StringUtils.parseInt(request.getParameter("active"),-1);
            if (wActive != 1)
                wActive = StringUtils.parseInt(request.getParameter("Active"),-1);

            int wGrad = StringUtils.parseInt(request.getParameter("grad"),-1);

            if (wGrad > 0 && (wBMSEmployee.Grad == UserGrads.System.getValue()
                    || wBMSEmployee.Grad == UserGrads.Engineer.getValue())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", Constants.GetEngineerUserList(), null);
                return wResult;
            }

            int wDepartmentID = StringUtils.parseInt(request.getParameter("DepartmentID"),-1);
            int wPosition = StringUtils.parseInt(request.getParameter("Position"),-1);
            int wPositionType = StringUtils.parseInt(request.getParameter("PositionType"),-1);

            int wDepartmentType = StringUtils.parseInt(request.getParameter("DepartmentType"),-1);
            String wName = StringUtils.parseString(request.getParameter("Name"));

            Pagination wPagination = GetPagination(request);

            ServiceResult<List<BMSEmployee>> wServerRst = wBMSService.BMS_QueryEmployeeList(wBMSEmployee, wDepartmentID,
                    wPosition, wDepartmentType, wPositionType, wActive, wName, wPagination);

            if (wServerRst != null && wServerRst.getResult() != null) {

                for (BMSEmployee wBmsEmployee : wServerRst.getResult()) {
                    wBmsEmployee.Password = "";
                }
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/IDAll")
    Object IDAll(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            List<Integer> wInteger = StringUtils
                    .parseIntList(StringUtils.split(StringUtils.parseString(request.getParameter("IDList")), ","));

            ServiceResult<List<BMSEmployee>> wServerRst = wBMSService.BMS_QueryEmployeeList(wBMSEmployee, wInteger);

            if (wServerRst != null && wServerRst.getResult() != null) {

                for (BMSEmployee wBmsEmployee : wServerRst.getResult()) {
                    wBmsEmployee.Password = "";
                }
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/Update")
    Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BMSEmployee wBMSEmployeeTemp = CloneTool.Clone(wParam.get("data"), BMSEmployee.class);

            if (wBMSEmployeeTemp.Grad == UserGrads.System.getValue()) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR,
                        StringUtils.Format("无权限操作{0}用户！", UserGrads.System.getLabel()));
                return wResult;
            }

            if (wBMSEmployeeTemp.Grad == UserGrads.Engineer.getValue()) {
                if (wBMSEmployee.Grad != UserGrads.System.getValue()) {
                    wResult = GetResult(RetCode.SERVER_CODE_ERR,
                            StringUtils.Format("无权限操作{0}用户！", UserGrads.Engineer.getLabel()));
                    return wResult;
                }
                List<BMSEmployee> wEngineerUserList = Constants.GetEngineerUserList();
                if (wEngineerUserList == null)
                    wEngineerUserList = new ArrayList<BMSEmployee>();
                Optional<BMSEmployee> wOptional = null;
                if (wBMSEmployeeTemp.ID > 0) {
                    wOptional = wEngineerUserList.stream().filter(p -> p.ID == wBMSEmployeeTemp.ID).findFirst();
                    if (!wOptional.isPresent()) {
                        wResult = GetResult(RetCode.SERVER_CODE_ERR,
                                StringUtils.Format("要修改的{0}用户不存在！", UserGrads.Engineer.getLabel()));
                        return wResult;
                    }
                    wEngineerUserList.removeIf(p -> p.ID == wBMSEmployeeTemp.ID);
                    wEngineerUserList.add(wBMSEmployeeTemp);
                    Constants.SetEngineerUserList(wEngineerUserList);
                } else {

                    for (BMSEmployee bmsEmployee : wEngineerUserList) {
                        if (bmsEmployee.ID >= wBMSEmployeeTemp.ID) {
                            wBMSEmployeeTemp.ID = bmsEmployee.ID;
                        }
                    }
                    if (wBMSEmployeeTemp.ID > 0) {
                        if (wBMSEmployeeTemp.ID == Integer.MAX_VALUE) {
                            wResult = GetResult(RetCode.SERVER_CODE_ERR,
                                    StringUtils.Format("{0}用户数量已达到最大值！", UserGrads.Engineer.getLabel()));
                            return wResult;
                        }
                        wBMSEmployeeTemp.ID++;

                    } else {
                        wBMSEmployeeTemp.ID = Constants.EngineerUserIDMin + 1;
                    }
                    wBMSEmployeeTemp.LoginName = wBMSEmployeeTemp.ID + "";
                    wBMSEmployeeTemp.Password = BaseConfigConstans.DefaultPassword;
                    wEngineerUserList.add(wBMSEmployeeTemp);
                    Constants.SetEngineerUserList(wEngineerUserList);

                }
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBMSEmployeeTemp);
                return wResult;

            }

            ServiceResult<Integer> wServerRst = new ServiceResult<Integer>(0);

            if (wBMSEmployeeTemp.ID > 0)

                wServerRst = wBMSService.BMS_SaveEmployee(wBMSEmployee, wBMSEmployeeTemp);
            else
                wServerRst = wBMSService.BMS_AddEmployee(wBMSEmployee, wBMSEmployeeTemp);

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBMSEmployeeTemp);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), null, wBMSEmployeeTemp);
            }
        } catch (

                Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/Active")
    Object Active(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            List<BMSEmployee> wBMSEmployeeList = CloneTool.CloneArray(wParam.get("data"), BMSEmployee.class);

            int wActive = wParam.containsKey("active") ? (int) wParam.get("active") : 0;

            if (wActive != 1) {
                wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;
            }
            if (wBMSEmployeeList == null || wBMSEmployeeList.size() < 1) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_IN);
                return wResult;
            }
            ServiceResult<Integer> wServerRst = new ServiceResult<Integer>(0);
            for (BMSEmployee wBMSEmployeeTemp : wBMSEmployeeList) {
                if (wBMSEmployee.ID <= 0)
                    continue;
                if (wActive == 1) {
                    wServerRst = wBMSService.BMS_ActiveEmployee(wBMSEmployee, wBMSEmployeeTemp);
                } else {
                    wServerRst = wBMSService.BMS_DisableEmployee(wBMSEmployee, wBMSEmployeeTemp);
                }
                if (StringUtils.isNotEmpty(wServerRst.getFaultCode()))
                    break;

            }
            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/Delete")
    Object Delete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            List<BMSEmployee> wBMSEmployeeList = CloneTool.CloneArray(wParam.get("data"), BMSEmployee.class);

            if (wBMSEmployeeList == null || wBMSEmployeeList.size() < 1) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_IN);
                return wResult;
            }
            ServiceResult<Integer> wServerRst = new ServiceResult<Integer>(0);
            for (BMSEmployee wBMSEmployeeTemp : wBMSEmployeeList) {
                if (wBMSEmployee.ID <= 0)
                    continue;
                if (wBMSEmployeeTemp.Active == 1)
                    continue;

                wServerRst = wBMSService.BMS_DeleteEmployee(wBMSEmployee, wBMSEmployeeTemp);

                if (StringUtils.isNotEmpty(wServerRst.getFaultCode()))
                    break;

            }
            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/InfoSecret")
    Object InfoSecretGet(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            int wModuleID = StringUtils.parseInt(request.getParameter("ModuleID"));

            if (wModuleID <= 0) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            BFCHomePageModule wHomePageModule = new BFCHomePageModule();
            ServiceResult<BFCHomePageModule> wServiceResult = wBFCService.BFC_GetHomePageModuleByID(wBMSEmployee,
                    wModuleID);

            if (wServiceResult == null || wServiceResult.getResult() == null
                    || StringUtils.isNotEmpty(wServiceResult.getFaultCode())
                    || wServiceResult.getResult().getID() != wModuleID) {

                wResult = GetResult(RetCode.SERVER_CODE_ERR,
                        "Module Not Find!" + "    " + wServiceResult.getFaultCode());

                return wResult;
            }

            wHomePageModule = wServiceResult.getResult();
            Map<String, Object> wRst = new HashMap<String, Object>();

            wRst.put("ID", DesUtil.encrypt(wBMSEmployee.getID() + "", wHomePageModule.getSecretKey()));
            wRst.put("LoginName", DesUtil.encrypt(wBMSEmployee.getLoginName(), wHomePageModule.getSecretKey()));
            wRst.put("LoginID", DesUtil.encrypt(wBMSEmployee.getLoginID() + "", wHomePageModule.getSecretKey()));
            wRst.put("Password", DesUtil.encrypt(wBMSEmployee.getPassword(), wHomePageModule.getSecretKey()));
            wRst.put("ModuleID", wModuleID);

            wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wRst);
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 获取登录人所在部门的人员列表
     */
    @GetMapping("/UserAllByLogin")
    public Object UserAllByLogin(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            ServiceResult<List<BMSEmployee>> wServiceResult = wBMSService.BMS_QueryUserAllByLogin(wLoginUser);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 导入
     */
    @PostMapping("/Import")
    public Object Import(HttpServletRequest request, @RequestParam("file") MultipartFile[] files) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            if (files.length == 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, "提示：没有要上传的Excel文件！");
            }

            ServiceResult<Integer> wServiceResult = new ServiceResult<>();
            ServiceResult<ExcelData> wExcelData = null;
            String wOriginalFileName = null;
            for (MultipartFile wMultipartFile : files) {
                wOriginalFileName = wMultipartFile.getOriginalFilename();

                assert wOriginalFileName != null;
                if (wOriginalFileName.contains("xlsx"))
                    wExcelData = ExcelReader.getInstance().readMultiSheetExcel(wMultipartFile.getInputStream(),
                            wOriginalFileName, "xlsx", 1000000);
                else if (wOriginalFileName.contains("xls"))
                    wExcelData = ExcelReader.getInstance().readMultiSheetExcel(wMultipartFile.getInputStream(),
                            wOriginalFileName, "xls", 1000000);

                assert wExcelData != null;
                if (StringUtils.isNotEmpty(wExcelData.FaultCode)) {
                    wResult = GetResult(RetCode.SERVER_CODE_ERR, wExcelData.FaultCode);
                    return wResult;
                }

                wServiceResult = wBMSService.BMS_ImportUser(wLoginUser, wExcelData.Result);

                if (!StringUtils.isEmpty(wServiceResult.FaultCode))
                    break;
            }

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "导入成功!", null, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 修改密码
     */
    @PostMapping("/PasswordModifyNew")
    Object PasswordModifyNew(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            String wUserAcc = StringUtils.parseString(wParam.get("Id"));
            String wPassword = StringUtils.parseString(wParam.get("PassWord"));
            String wOldPassWord = StringUtils.parseString(wParam.get("OldPassWord"));

            if (StringUtils.isEmpty(wPassword) || wPassword.trim().length() < 6 || wPassword.trim().length() > 12) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, "新密码不符合标准！（长度6-12的字母和数字）");
                return wResult;
            }

            ServiceResult<Integer> wServerRst = wBMSService.BMS_PasswordModifyNew(wLoginUser, wUserAcc, wPassword,
                    wOldPassWord);

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), null, null);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_SUC, ex.toString(), null, null);
        }
        return wResult;
    }
}
